/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "queue.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
void led_flash(void);
int keyScan(void);
/* USER CODE END Variables */
/* Definitions for Task_Led */
osThreadId_t Task_LedHandle;
const osThreadAttr_t Task_Led_attributes = {
  .name = "Task_Led",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for Task_ScanKey */
osThreadId_t Task_ScanKeyHandle;
const osThreadAttr_t Task_ScanKey_attributes = {
  .name = "Task_ScanKey",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Queue_Key */
osMessageQueueId_t Queue_KeyHandle;
const osMessageQueueAttr_t Queue_Key_attributes = {
  .name = "Queue_Key"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_Led(void *argument);
void AppTask_ScanKey(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of Queue_Key */
  Queue_KeyHandle = osMessageQueueNew (16, sizeof(uint8_t), &Queue_Key_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Task_Led */
  Task_LedHandle = osThreadNew(AppTask_Led, NULL, &Task_Led_attributes);

  /* creation of Task_ScanKey */
  Task_ScanKeyHandle = osThreadNew(AppTask_ScanKey, NULL, &Task_ScanKey_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_Led */
/**
  * @brief  Function implementing the Task_Led thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_AppTask_Led */
void AppTask_Led(void *argument)
{
  /* USER CODE BEGIN AppTask_Led */
  uint8_t receivedValue = 0;
  printf("ledled-------\n");
  /* Infinite loop */
  for(;;)
  {
    xQueueReceive(Queue_KeyHandle, &receivedValue, 0);
    //printf("receivedValue:%c----\n",&receivedValue);
    if(receivedValue == 1)
    {
			receivedValue = 1;
      led_flash();
      led_flash();
      led_flash();
      receivedValue = 0;   //清零
    }else if(receivedValue == 2){
      HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
      receivedValue = 0;   //清零
    }else{

    }
		osDelay(50);   //让出cpu
  }
  /* USER CODE END AppTask_Led */
}

/* USER CODE BEGIN Header_AppTask_ScanKey */
/**
* @brief Function implementing the Task_ScanKey thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_AppTask_ScanKey */
void AppTask_ScanKey(void *argument)
{
  /* USER CODE BEGIN AppTask_ScanKey */
  printf("进入keysacn函数----\n");
  /* Infinite loop */
  for(;;)
  {
    keyScan();
  }
  /* USER CODE END AppTask_ScanKey */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
uint32_t count = 0;
int keyScan(void){
  
  uint8_t key_status = 0;
  for(uint8_t i = 0; i<100; i++)
  {
    printf("run count: %d\n",count);
    if(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin) == GPIO_PIN_RESET)
    {
      count++;
    }
    if(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin) == GPIO_PIN_SET)
    {
      if(count > 90)
      {
        //长按
        
        key_status = 1; 
        xQueueSendToBack(Queue_KeyHandle, &key_status, 0);
      }else if(count > 2)
      {
        //短按
        
        key_status = 2; 
        xQueueSendToBack(Queue_KeyHandle, &key_status, 0);  
      }else
      {
        //抖动,不做处理
      }
      count = 0;   //计数器清零
    }
    osDelay(10);
  }
  return key_status;
}

void led_flash(void)
{
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
  osDelay(500);
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
  osDelay(500);
}
/* USER CODE END Application */

